package com.limanyik.family.oss.message.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.limanyik.family.common.entity.server.base.dto.MobilePhoneMessageDto;
import com.limanyik.family.common.entity.server.base.vo.MobilePhoneMessageVo;
import com.limanyik.family.oss.message.service.MessageService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;

@Service(value = "aliyunMessageServiceImpl")
public class AliyunMessageServiceImpl implements MessageService {

    @Value("${aliyun.message.accessKeyId}")
    private String ACCESS_KEY_ID;
    @Value("${aliyun.message.accessKeySecret}")
    private String ACCESS_KEY_SECRET;
    private static Client client;

    /**
     * aliyun配置模板
     */
    private static String TEMPLATE_PARAM = "SMS_170116124";

    /**
     * 阿里云短信签名
     */
    private static String SIGN_NAME = "常常气团";

    @PostConstruct
    @SneakyThrows
    private void init() {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(ACCESS_KEY_ID)
                // 您的AccessKey Secret
                .setAccessKeySecret(ACCESS_KEY_SECRET);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        client = new Client(config);
    }

    /**
     * 发送通用短信
     *
     * @param mobilePhoneMessageVo
     */
    @SneakyThrows
    @Override
    public void sendMessage(MobilePhoneMessageVo mobilePhoneMessageVo) {

    }

    /**
     * 发送验证码
     *
     * @param mobilePhoneMessageVo
     */
    @SneakyThrows
    @Override
    public MobilePhoneMessageDto verificationCode(MobilePhoneMessageVo mobilePhoneMessageVo) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setSignName(SIGN_NAME)
                .setTemplateCode(TEMPLATE_PARAM)
                .setPhoneNumbers(mobilePhoneMessageVo.getPhone())
                .setTemplateParam("{'code':'" + mobilePhoneMessageVo.getMessage() + "'}");
        SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
        System.out.println(JSONObject.toJSONString(sendSmsResponse));
        return new MobilePhoneMessageDto<MobilePhoneMessageVo>()
                .setMessage(sendSmsResponse.getBody().getMessage())
                .setCode(sendSmsResponse.getBody().getCode())
                .setBusinessId(sendSmsResponse.getBody().getBizId())
                .setRequestId(sendSmsResponse.getBody().getRequestId())
                .setResponseTime(LocalDateTime.now())
                .setOperatorId("1")
                .setOperator("sysAdmin")
                .setRequestTime(LocalDateTime.now())
                .setRequestBody(mobilePhoneMessageVo);
    }
}
